import Vue from 'vue'
import VueRouter from 'vue-router'
/* import Login from '../views/login.vue'
import Layout from '../views/layout/layout.vue'
import Article from '../views/articles/article.vue'
import Image from '../views/images/image.vue'
import Page404 from '../views/page404/page404.vue'
import AddArticle from '../views/articles/add.vue' */
const Login = () => import('../views/login.vue')
const Layout = () => import('../views/layout/layout.vue')
const Article = () => import('../views/articles/article.vue')
const AddArticle = () => import('../views/articles/add.vue')
const Image = () => import('../views/images/image.vue')
const Page404 = () => import('../views/page404/page404.vue')
Vue.use(VueRouter)
// 配置路由
const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        // 内容管理
        path: '/articles',
        component: Article
      },
      // 发布文章
      {
        path: '/addArticle',
        component: AddArticle
      },
      {
        // 素材管理
        path: 'image',
        component: Image
      }
    ]
  },
  {
    path: '*',
    component: Page404
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  console.log(Date.now(), '路由跳转了')
  console.log(to, from, next)
  // 如果你要去的页面是 "VIP" 页面
  const pageList = ['/', '/image', '/articles', '/addArticle']
  // if (to.path === '/image' || to.path === '/article') {
  if (pageList.includes(to.path)) {
    // 1. 获取token
    const tokenStr = localStorage.getItem('tokenStr')
    if (tokenStr) {
      next() // 放行
    } else {
      console.log('大胆狂徒，没有VIP，这里不让你进！！！！！')
      next('/login')
    }
  } else {
    next() // 放行
  }
})

export default router
